package com.yuesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuesystem.common.R;
import com.yuesystem.dto.Link;
import com.yuesystem.dto.Point;
import com.yuesystem.entity.FixedItem3;
import com.yuesystem.entity.Office;
import com.yuesystem.entity.OfficeItem;
import com.yuesystem.service.FixedItem3Service;
import com.yuesystem.service.OfficeItemService;
import com.yuesystem.service.OfficeService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 可视化控制类.
 */
@RestController
@RequestMapping("/visual2")
@CrossOrigin
@Slf4j
public class VisualController2 {
  @Autowired
  private OfficeService officeService;
  
  @Autowired
  private FixedItem3Service fixedItem3Service;
  
  @Autowired
  private OfficeItemService officeItemService;
  
  /**
   * 朝代-官职.
   */
  @GetMapping("/getLink1")
  public R<List<Link>> getLink1() {
    String[] dynasty = {
        "汉代", "三国魏", "晋代", "南朝宋", "南朝梁", "南朝陈", "北魏", "北齐", "北周",
        "隋朝", "唐朝", "宋代", "金朝", "元代", "明代", "清代"
    };
    List<Link> list = new ArrayList<>();
    for (int i = 0; i < dynasty.length; i++) {
      LambdaQueryWrapper<FixedItem3> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(FixedItem3::getDynasty, dynasty[i]);
      List<FixedItem3> items = fixedItem3Service.list(queryWrapper);
      int finalI = i;
      List<Link> links = items.stream().map((item) -> {
        Link link = new Link();
        link.setSource(dynasty[finalI]);
        link.setTarget(String.valueOf(item.getId()));
        return link;
      }).collect(Collectors.toList());
      list.addAll(links);
    }
    return R.success(list);
  }
  
  
  /**
   * 品阶节点.
   */
  @GetMapping("/getPoint1")
  public R<List<Point>> getPoint1() {
    List<String> type = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8",
        "9", "10", "11", "12", "13", "14", "15", "16");
    String[] dynasties = {
        "汉代", "三国魏", "晋代", "南朝宋", "南朝梁", "南朝陈", "北魏", "北齐",
        "北周", "隋朝", "唐朝", "宋代", "金朝", "元代", "明代", "清代"
    };
    
    LambdaQueryWrapper<FixedItem3> queryWrapper = new LambdaQueryWrapper<>();
    List<FixedItem3> list = fixedItem3Service.list(queryWrapper);
    List<Point> points = list.stream().map((item) -> {
      Point point = new Point();
      point.setSymbolSize(20);
      point.setName(item.getName());
      point.setId(item.getId());
      
      String place = item.getDynasty();
      String mytype = "";
      for (int i = 0; i < 16; i++) {
        if (place.contains(dynasties[i])) {
          mytype = type.get(i);
          break;
        }
      }
      point.setCategory(mytype);
      return point;
    }).collect(Collectors.toList());
    return R.success(points);
  }
  
  
  /**
   * 根据官职id查找节点.
   *
   * @param id 官职id
   * @return 官职id对应的节点
   */
  @GetMapping("/getOfficePoint/{id}")
  public R<List<Point>> getOfficePoint(@PathVariable Long id) {
    //增加官职主体节点
    Office office = officeService.getById(id);
    Point p1 = new Point();
    p1.setId(id);
    p1.setSymbolSize(60);
    p1.setName(office.getOfficeName());
    List<Point> list = new ArrayList<>();
    list.add(p1);
    //增加官职客体节点
    LambdaQueryWrapper<OfficeItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(OfficeItem::getOfficeId, id)
        .eq(OfficeItem::getIsDeleted, 0);
    List<OfficeItem> items = officeItemService.list(queryWrapper);
    List<Point> p2 = items.stream().map((item) -> {
      Point point = new Point();
      point.setId(2000 + item.getId());
      point.setSymbolSize(30);
      point.setName(item.getObject() != null ? item.getObject() : "客体未详");
      return point;
    }).collect(Collectors.toList());
    
    //结合
    list.addAll(p2);
    return R.success(list);
  }
  
  /**
   * 获得单个官职的联系.
   *
   * @param id 官职id
   * @return 官职的联系
   */
  @GetMapping("/getOfficeLink/{id}")
  public R<List<Link>> getOfficeLink(@PathVariable Long id) {
    LambdaQueryWrapper<OfficeItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(OfficeItem::getOfficeId, id)
        .eq(OfficeItem::getIsDeleted, 0);
    List<OfficeItem> items = officeItemService.list(queryWrapper);
    List<Link> list = items.stream().map((item) -> {
      Link link = new Link();
      link.setSource(String.valueOf(id));
      link.setTarget(String.valueOf(item.getId() + 2000));
      link.setValue(item.getRelation());
      return link;
    }).collect(Collectors.toList());
    return R.success(list);
  }
}
